package com.digitalcollege.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.SecurityFilterChain;

import javax.annotation.Resource;
import java.util.List;

/**
 * @class: SecurityConfig
 * @author: liyusheng
 * @description:
 * @date: 2022/12/17 13:41
 */
@Configuration
public class SecurityConfig {
    @Resource
    IgnoreUrlConfig ignoreUrlConfig;

    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws Exception {
        ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry registry = httpSecurity.authorizeRequests();
        List<String> ignoreUrlList = ignoreUrlConfig.getIgnoreUrlList();
        for (String ignoreUrl : ignoreUrlList) {
            registry.antMatchers(ignoreUrl).permitAll();
        }
        return httpSecurity.cors().and().csrf().disable().authorizeRequests().anyRequest()
                .authenticated()
                .and()
                .sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
                .headers().cacheControl()
                .and()
                .and()
                .build();
    }
}
